package com.aiit.controller.admin;


import com.aiit.entity.CreateBookingTime;
import com.aiit.service.BookingService;
import com.aiit.service.CreateBookingTimeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


//自动生成时间的定时扫描接口
@Component
@Slf4j
public class CreateBookingTimeController {

    @Autowired
    private BookingService bookingService;

    @Autowired
    private CreateBookingTimeService createBookingTimeService;


    @Scheduled(cron = "30 0 10,14,16,19 * * ?")
    public void createBookingTimeController() {
        CreateBookingTime createBookingTime = new CreateBookingTime();
        createBookingTimeService.insert(createBookingTime);
    }

    @Scheduled(cron = "0 * * * * ?")
    public void scanAndUpdateBookings() {
        try {
            int updatedRows = bookingService.updateBookingsWithOldSessionTime();
            log.info("Updated {} bookings.", updatedRows);
        } catch (Exception e) {
            log.error("An error occurred while updating bookings: ", e);
        }
    }

    @Scheduled(cron = "0 * * * * ?")
    public void scanAndUpdateUserBooking() {
        try {
            int updatedRows = bookingService.updateUserBookingWithOldSessionTime();
            log.info("Updated {} bookings.", updatedRows);
        } catch (Exception e) {
            log.error("An error occurred while updating bookings: ", e);
        }
    }

    @Scheduled(cron = "0/1 * * * * ?")
    public void scanAndUpdateBookingStatus() {
        try {
            int updatedRows = bookingService.updateUserBookingStatus();
            log.info("Updated {} bookings.", updatedRows);
        } catch (Exception e) {
            log.error("An error occurred while updating bookings: ", e);
        }
    }

}
